/**
 * 图鉴界面
 */
class TujianUI extends UIBase{
	private btn_close:BaseButton;
    private img_icon:eui.Image;
    private lb_name:eui.Label;
    private lb_desc:eui.Label;
    private fish_scroller:eui.Scroller;
    private fish_list:eui.List;

	private _listDataArr:eui.ArrayCollection;
	public curSelectItem:TujianItem;
	private lb_price:eui.Label;
	private lb_progress:eui.Label;
	private main_group:eui.Group;

	private count:number;

	public constructor(data?) {
        data.showBG = true;
		super('TujianView', data);
		this.curSelectItem = null;
	}

	public init()
	{
		this.count = 0;
		TujianPrase.instance().getTujianInfo(this.initUI, this);
		this.fixIphoneX();
	}

	//屏幕适配x
    protected fixIphoneX()
    {
		if(GlobalManager.instance().isIphonex())
		this.main_group.top = Math.floor(GlobalManager.instance().curHeight()/34);
    }

	public initUI(data:Array<string>){
		let fishArr = FishTpl.instance().getDataAsArray();

		this.fish_list.useVirtualLayout = false;
		this.fish_scroller.viewport.scrollV = 0;
		let hasLockIdList = data;

		let Arr = [];
		let arr = [];
		
		for(let k in fishArr)
		{		
			let data = fishArr[k];
			
			if(hasLockIdList.indexOf((parseInt(k)+1).toString()) != -1)
			{
				data['isLocked'] = 0;
				this.count ++;
			}
			else
			{
				data['isLocked'] = 1;
			}
		}

		//排序 按照是否解锁排 再按id排
		fishArr.sort(function(a, b){
			if(a['isLocked'] == b['isLocked'])
				return a["id"] - b["id"];
			else
				return a['isLocked'] - b['isLocked'];
		});

		for(let k in fishArr)
		{		
			let data = fishArr[k];
			if(arr.length < 4)
			{
				//四个为一组
				arr.push(data);
				if(parseInt(k)+1 == fishArr.length)
				{
					Arr.push(arr);
					arr = [];
				}
			}
			else
			{
				Arr.push(arr);
				arr = [];
				arr.push(data);
			}
		}
		
		this._listDataArr = new eui.ArrayCollection(Arr);

		this.fish_list.dataProvider = this._listDataArr;
		this.fish_list.itemRenderer = TujianBigItem;
		this.lb_progress.text = this.count + " / " + fishArr.length;
	}

	public addEvent(){
		super.addEvent();
		this.btn_close.addTapHandler(this.btnClose, this);
		GameUtil.addEventListener(EventConst.TUJIAN_SELECT, this.update,this);
	}

	protected update(e)
	{
		let item = e.param as TujianItem;
		if(item == this.curSelectItem) return;
		item.img_select.visible = true;

		if(this.curSelectItem)
		{
			this.curSelectItem.img_select.visible = false;
		}
		this.curSelectItem = item;
		let data = item.data;
		this.img_icon.source = data["icon"];
		this.lb_desc.text = data["desc"];
		this.lb_name.text = data["name"];
		this.lb_price.text = data["price"];
	}

	public removeEvent(){
		super.removeEvent();
		this.btn_close.removeTapHandler();
		GameUtil.removeEventListener(EventConst.TUJIAN_SELECT, this.update,this);
	}

	private btnClose(){
		this.remove(1);
	}

	public clear(){
		super.clear();
		this.curSelectItem = null;
		this.fish_list.dataProvider = null;
		this.fish_list.itemRenderer = null;
		this.curSelectItem = null;
	}
}